﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Data.SqlClient;
using HotelManager.DataAccess;
using HotelManager.Model;

namespace HotelManager.View
{
    public partial class FormVatDung : Form
    {
        VatdungAccess vatDungAccess = new VatdungAccess();
        DataTable dsDD = new DataTable();
        public FormVatDung()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void cbbTimkiem_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FormVatDung_Load(object sender, EventArgs e)
        {
            dsDD = vatDungAccess.laytoanboDD();
            dataGridView1.DataSource = dsDD;
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            VatDungPhong dd = new VatDungPhong(txtMavatdung.Text, txtMaphong.Text, txtTenvatdung.Text, int.Parse(txtSoLuong.Text), txtGhichu.Text);
            if (vatDungAccess.suaDD(dd) == 0)
            {
                MessageBox.Show("Mã đồ dùng không tồn tại!");
            }
            else
            {
                MessageBox.Show("Sửa thành công!");
            }
            FormVatDung_Load(sender, e);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMavatdung.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtMaphong.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtTenvatdung.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtSoLuong.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtGhichu.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            DataView dataView = new DataView();
            if (cbbTimkiem.SelectedItem == cbbTimkiem.Items[0])
            {
                dataView = vatDungAccess.timkiemMaDD(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }
            else
            {
                dataView = vatDungAccess.timkiemTenDD(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            VatDungPhong dd = new VatDungPhong(txtMavatdung.Text, txtMaphong.Text, txtTenvatdung.Text, int.Parse(txtSoLuong.Text), txtGhichu.Text);
            if (vatDungAccess.xoaDD(dd) == 0)
            {
                MessageBox.Show("Mã đồ dùng không tồn tại!");
            }
            else
            {
                MessageBox.Show("Xóa thành công! ");
            }
            FormVatDung_Load(sender, e);

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            VatDungPhong dd = new VatDungPhong(txtMavatdung.Text, txtMaphong.Text, txtTenvatdung.Text, int.Parse(txtSoLuong.Text), txtGhichu.Text);
            if (vatDungAccess.themDD(dd) == 0)
            {
                MessageBox.Show("Mã đồ dùng đã tồn tại!");
            }
            else
            {
                MessageBox.Show("Thêm thành công!");
            }
            FormVatDung_Load(sender, e);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                txtMavatdung.ReadOnly = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtMavatdung.ReadOnly = true;
                btnThem.Enabled = false;
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
